package com.universal.frame.generalutils.adapterutil;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.universal.frame.R;

public class AdapterViewHolder {

    /**
     * 异步加载图片统一配置项
     */
    private static DisplayImageOptions options;

    private final SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;

    private AdapterViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
        this.mPosition = position;
        this.mViews = new SparseArray<View>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        // setTag
        mConvertView.setTag(this);
    }

    /**
     * 拿到一个ViewHolder对象
     *
     * @param context
     * @param convertView
     * @param parent
     * @param layoutId
     * @param position
     * @return
     */
    public static AdapterViewHolder get(Context context, View convertView,
                                        ViewGroup parent, int layoutId, int position) {
        AdapterViewHolder holder = null;
        if (convertView == null) {
            holder = new AdapterViewHolder(context, parent, layoutId, position);
        } else {
            holder = (AdapterViewHolder) convertView.getTag();
            holder.mPosition = position;
        }
        return holder;
    }

    public View getConvertView() {
        return mConvertView;
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public AdapterViewHolder setText(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public AdapterViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);

        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @return
     */
    public AdapterViewHolder setImageBitmap(int viewId, Bitmap bm) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @return
     */
    public AdapterViewHolder setImageByUrl(int viewId, String url) {
        ImageLoader.getInstance().displayImage(url,
                (ImageView) getView(viewId), getOptions());
        return this;
    }

    public int getPosition() {
        return mPosition;
    }

    @SuppressWarnings("unchecked")
    public static <T extends View> T get(View view, int id) {
        SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
        if (viewHolder == null) {
            viewHolder = new SparseArray<View>();
            view.setTag(viewHolder);
        }
        View childView = viewHolder.get(id);
        if (childView == null) {
            childView = view.findViewById(id);
            viewHolder.put(id, childView);
        }
        return (T) childView;
    }


    /**
     * 获取ImageLoader的options
     */
    public DisplayImageOptions getOptions() {
        if (null == options) {
            options = new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.imageloader_on_img)
                    .showImageForEmptyUri(R.mipmap.imageloader_empty_img).showImageOnFail(R.mipmap.imageloader_fail_img)
                    .cacheInMemory(true).cacheOnDisk(true).considerExifParams(true).build();
        }
        return options;
    }

}
